#!/usr/bin/env python
# -*- coding: UTF-8 -*-

'''Benchmark the Game of Life implementation'''

import os
import sys
import cProfile
import cPickle as pickle
from timeit import Timer

from cellular_engine.constants import WORLD_TYPE_TORUS
from game_of_life import GameOfLife

FILENAME = 'gameoflife_small.pickle'
BENCH_FILE = os.path.join(os.path.dirname(__file__), 'benchmark', FILENAME)
BENCH_RUNS = 10

GAME_SIZE = (1024, 1024)
TILE_SIZE = (1, 1)
WORLD_TYPE = WORLD_TYPE_TORUS

with open(BENCH_FILE) as f:
    attr = pickle.load(f)
    the_game = GameOfLife.import_dictionary(attr)


def provide_profile_data():
    for _ in xrange(BENCH_RUNS):
        the_game.game_loop()


def time_it():
    t = Timer("provide_profile_data()", "from __main__ import provide_profile_data")
    print t.timeit(number=50)


def generate_profile():
    command = "provide_profile_data()"
    cProfile.runctx(command, globals(), locals(), filename=FILENAME.replace('.pickle', '.profile'))

if __name__ == '__main__':
    arg = sys.argv[1].lower()

    if arg == 'time':
        time_it()
    elif arg == 'profile':
        generate_profile()

